package com.bdqn.t382.lx.controller;

import com.alibaba.fastjson2.JSON;
import com.bdqn.t382.lx.mapper.AnimalsMapper;
import com.bdqn.t382.lx.pojo.Animals;
import com.bdqn.t382.lx.pojo.BookManage;
import com.bdqn.t382.lx.pojo.Page;
import com.bdqn.t382.lx.pojo.SysUser;
import com.bdqn.t382.lx.service.AnimalsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/anima")
public class AnimalsController {
    private Logger logger = LoggerFactory.getLogger(BookManageController.class);

    @Autowired
    AnimalsService animalsService;

    @ResponseBody
    @RequestMapping("/doLogin")
    public SysUser doLogin(String account, String password) {
        SysUser user = animalsService.getLogin(account, password);
        return user;
    }

    @ResponseBody
    @RequestMapping("/page")
    public Page page(
                     @RequestParam Integer pageNo,
                     @RequestParam Integer pageSize, @RequestParam String ani_name) {
        Page page= animalsService.getPage(pageNo, pageSize,ani_name);
        return page;
    }

    @ResponseBody
    @RequestMapping("/list")
    public List list() {
        List<Animals> list = animalsService.getAnimalsList();
        return list;
    }

    @ResponseBody
    @RequestMapping("/formAdd")
    public int formAdd( @RequestBody Animals animals) {
        int count = animalsService.insertAnimals(animals);
        return count;
    }


    @ResponseBody
    @RequestMapping("/delete")
    public int delete( @RequestParam int ani_id) {
        int count = animalsService.deleteAnimals(ani_id);
        return count;
    }

    @ResponseBody
    @RequestMapping("/view")
    public Animals view(Model model, @RequestParam int ani_id) {
        // 根据 b_id 查询图书信息
        Animals animals = animalsService.viewAnimals(ani_id);
        model.addAttribute("animals", animals);
        return animals;
    }

    @ResponseBody
    @RequestMapping("/formUpdate")
    public int formView(@RequestBody Animals animals) {
        int count = animalsService.updateAnimals(animals);
        return count;
    }

    @ResponseBody
    @RequestMapping("/getSysuserById")
    public SysUser getSysuserById(@RequestParam int id) {
        SysUser user = animalsService.getSysuserById(id);
        return user;
    }

    @ResponseBody
    @RequestMapping("/accountExist")
    public String accountExist(SysUser sysUser) {
        HashMap<String, Object> resultMap = new HashMap();
        SysUser login = animalsService.getLogin(sysUser.getAccount(), sysUser.getPassword());
        if (login != null) {
            resultMap.put("exist", 1);
        } else {
            resultMap.put("exist", 0);
        }
        return JSON.toJSONString(resultMap);
    }
}
